package org.footito.android.api;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * @author jdelrio
 * 
 */
public class CommentsApiHandler extends DefaultHandler {

	public final static String URL_API = "http://api.footito.fr/comment/list";
	public final static String URL_API_PARAM_LIMIT = "limit";
	public final static String URL_API_PUBLISHED_AFTER = "published_after";

	private String commentContent;
	private int commentId;
	private int commentIdTeam;
	private int commentIdEvent;
	private ArrayList<CommentFromAPI> mCommentFromAPIsList = new ArrayList<CommentFromAPI>();
	private int positive_likes_status;
	private int negative_likes_status;

	/**
	 * 
	 */
	public CommentsApiHandler() {
		mCommentFromAPIsList = new ArrayList<CommentFromAPI>();
	}
	
	public ArrayList<CommentFromAPI> getCommentsFromAPIsList() {
		return mCommentFromAPIsList;
	}

	@Override
	public void startDocument() throws SAXException {
	}

	@Override
	public void endDocument() throws SAXException {
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if (localName.equals(TAG.timelineelement)) {
			IN.timelineElement = true;
		} else if (localName.equals(TAG.comment)) {
			IN.comment = true;
			this.commentContent = "";
			this.commentIdEvent = Integer.parseInt(attributes.getValue(TAG.commentidparent));
			this.commentId = Integer.parseInt(attributes.getValue(TAG.commentid));
			this.commentIdTeam = Integer.parseInt(attributes.getValue(TAG.commentidteam));
			this.positive_likes_status = Integer.parseInt(attributes.getValue(TAG.positive_likes));
			this.negative_likes_status = Integer.parseInt(attributes.getValue(TAG.negative_likes));
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if (localName.equals(TAG.timelineelement)) {
			IN.timelineElement = false;
		} else if (localName.equals(TAG.comment)) {
			IN.comment = false;
			CommentFromAPI comment = new CommentFromAPI(commentId, commentContent, commentIdTeam, commentIdEvent,
					positive_likes_status, negative_likes_status);
			if (comment.isValid())
				mCommentFromAPIsList.add(comment);
		}
	}

	public void characters(char[] ch, int start, int length) throws SAXException {
		if (IN.comment) {
			this.commentContent += new String(ch, start, length);
		}
	};

	private static class TAG {
		private final static String positive_likes = "positive_likes";
		private final static String negative_likes = "negative_likes";
		private static final String timelineelement = "timelineElements";
		private static final String comment = "comment";
		private static final String commentid = "id";
		private static final String commentidparent = "idParent";
		private static final String commentidteam = "idTeam";
	}

	private static class IN {
		private static boolean timelineElement;
		private static boolean comment;
	}
}
